package com.lanou.musicproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;


import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.ViedeoActivity;
import com.lanou.musicproject.adapter.MvAdapter;
import com.lanou.musicproject.bean.Mv;
import com.lanou.musicproject.tools.NoScrollGridView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 王世美 on 15/10/22.
 * mv页面
 */
public class MvFragment extends Fragment {
    private NoScrollGridView songListGV;
    // 初始化组件
  private TextView allTv;
//    @Bind(R.id.songListGV)
//    NoScrollGridView songListGV;
    // mv适配器成员变量
    private MvAdapter adapter;
    // request请求队列
    private RequestQueue requestQueue;
    // 定义一个实体类数据数组
    private List<Mv.ResultEntity.MvListEntity> data;
    // 请求数据接口网址
    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=" +
            "android&version=5.6.5.0&method=baidu.ting.mv.searchMV&format=" +
            "json&order=1&page_num=1&page_size=20&query=%E5%85%A8%E9%83%A8";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // 加载布局
        View v = inflater.inflate(R.layout.fragment_song_list, null);
        allTv = (TextView) v.findViewById(R.id.allTv);
        songListGV = (NoScrollGridView) v.findViewById(R.id.songListGV);
        allTv.setText(R.string.all);
        // 初始化队列
        requestQueue = Volley.newRequestQueue(getActivity());

        // 设置监听事件
        songListGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Mv.ResultEntity.MvListEntity entity = (Mv.ResultEntity.MvListEntity)
                        parent.getItemAtPosition(position);
                // 页面跳转
                Intent intent = new Intent();
                intent.setClass(getActivity(), ViedeoActivity.class);
                // 传递数据
                intent.putExtra("entity", entity);
                startActivity(intent);
            }
        });
        return v;


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = Volley.newRequestQueue(getActivity());
        //调用addData方法
        this.addData();
    }

    // 解析实体类方法
    public void addData() {
        String getUrl = path;
        StringRequest request = new StringRequest(getUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (response != null) {
                            Gson gson = new Gson();
                            Mv result = gson.fromJson(response,
                                    Mv.class);
                            if (result != null && (
                                    data = result.getResult().getMv_list()) != null) {
                                adapter = new MvAdapter(getActivity(), data);
                                songListGV.setAdapter(adapter);
                            }
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }

        });
        requestQueue.add(request);

    }

}
